﻿using System;
using Quartz.Core;
using Topshelf;
using ZilLion.Core.ConfigManager.Manager;
using ZilLion.TaskManager.Core;
using ZilLion.TaskManager.Core.Infrastructure;
using ZilLion.TaskManager.Entity.Config;

namespace ZilLion.Core.TaskManager.Client
{
    internal class Program
    {
        public static void Main()
        {
            HostFactory.Run(x =>
            {
                var config = ZilLionConfigurationManager.GetConfigInstance<TaskManagerCommonConfig>();
                if (config == null)
                    CustomTaskLogger.Print(new Exception("获取clinetconfig失败"));
                try
                {
                    x.SetDescription(config.ServiceDescription); //设置服务显示备注
                    x.SetDisplayName(config.ServiceDisplayName); //设置服务显示名称
                    x.SetServiceName($"TaskNode_{config.ClientNo.Replace("-", string.Empty)}"); //服务名称
                    x.Service<TaskHostService>(); //设置启动类
                    x.RunAsLocalSystem(); //使用本地系统用户
                    x.EnableShutdown(); //允许关闭
                    x.EnablePauseAndContinue(); //允许暂停和继续
                    x.StartAutomaticallyDelayed(); //延迟启动
                    x.OnException(exception =>
                        {
                            CustomTaskLogger.Print(exception,
                                $"TaskNode_{config.ClientNo.Replace(" - ", string.Empty)}作业服务运行出错!");
                        }
                    ); //全局错误回调
                }
                catch (Exception ex)
                {
                    CustomTaskLogger.Print(ex,
                        $"TaskNode_{config.ClientNo.Replace(" - ", string.Empty)}作业服务启动失败!");
                    Console.WriteLine(ex);
                    Console.ReadLine();
                }
            });
        }
    }
}